#include "edge_gradient.h"

EdgeGradient::EdgeGradient(PNM* img, ImageViewer* iv) :
    Convolution(img, iv)
{
}

EdgeGradient::EdgeGradient(PNM* img) :
    Convolution(img)
{
}

PNM* EdgeGradient::verticalDetection()
{
    return convolute(g_y, RepeatEdge);
}

PNM* EdgeGradient::horizontalDetection()
{
    return convolute(g_x, RepeatEdge);
}

PNM* EdgeGradient::transform()
{
    PNM* newImage = new PNM(image->width(), image->height(), image->format());

	auto ximage = horizontalDetection();
	auto yimage = verticalDetection();

	int width = image->width();
	int height = image->height();

	if (image->format() == QImage::Format_Indexed8)
    {
        // Iterate over image space
        for (int x=0; x<width; x++)
            for (int y=0; y<height; y++)
            {
				auto xpixel = ximage->pixel(x, y);
				auto ypixel = yimage->pixel(x, y);

				int v = sqrt( pow(qGray(xpixel),2) + pow(qGray(ypixel),2) );

                newImage->setPixel(x,y, v);
            }
    }
    else //if (image->format() == QImage::Format_RGB32)
    {
        // Iterate over image space
        for (int x=0; x<width; x++)
            for (int y=0; y<height; y++)
            {
				auto xpixel = ximage->pixel(x, y);
				auto ypixel = yimage->pixel(x, y);

				auto r = sqrt( pow(qRed(xpixel),2) + pow(qRed(ypixel),2) );
				auto g = sqrt( pow(qGreen(xpixel),2) + pow(qGreen(ypixel),2) );
				auto b = sqrt( pow(qBlue(xpixel),2) + pow(qBlue(ypixel),2) );
				
				QColor newPixel = QColor(r,g,b);
                newImage->setPixel(x,y, newPixel.rgb());
            }
    }

	return newImage;
}

